package lms.gui.admin;

import lms.Config;
import lms.entity.Entity;
import lms.entity.Item;
import lms.entity.Loan;
import lms.gui.member.EntityPanel;
import lms.gui.member.EntityPanelContainer;
import lms.manager.MemberMgr;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JSeparator;

public class LoanRequestPanel extends EntityPanel {
	private Loan loan;
	
	private JLabel lblImage;
	private JLabel lblTitle;
	private JLabel lblAuthor;
	private JLabel lblIsbn;

	private JSeparator separator;
	
	private JLabel lblBorrower;
	private JLabel lblT;
	private JLabel lblA;
	private JLabel label;
	private JLabel label_1;
	
	
	public LoanRequestPanel() {
		initGUI();
	}
	private void initGUI() {
		setLayout(null);
		
		lblImage = new JLabel("");
		lblImage.setBounds(5,5,Config.IMAGE_WIDTH/2,Config.IMAGE_HIGHT/2);
		add(lblImage);
		
		lblTitle = new JLabel("Title");
		lblTitle.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		lblTitle.setBounds(203, 5, 176, 22);
		add(lblTitle);
		
		lblAuthor = new JLabel("Author");
		lblAuthor.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		lblAuthor.setBounds(203, 30, 128, 22);
		add(lblAuthor);
		
		lblIsbn = new JLabel("Isbn");
		lblIsbn.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		lblIsbn.setBounds(203, 55, 138, 22);
		add(lblIsbn);
		
		lblBorrower = new JLabel("Borrower");
		lblBorrower.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		lblBorrower.setBounds(203, 80, 138, 22);
		add(lblBorrower);
		
		
		JButton btnCheckOut = new JButton("Check Out");
		btnCheckOut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String message = "Comfirm to Check Out?";
				int reply = JOptionPane.showConfirmDialog(null, message, Config.TITLE_WARNING, JOptionPane.YES_NO_OPTION);
				if (reply == JOptionPane.YES_OPTION) {
					try {
						accept();
					} catch (Exception exception) {
						exception.printStackTrace();
						JOptionPane.showMessageDialog(null, exception.getMessage(), Config.TITLE_ERROR, JOptionPane.ERROR_MESSAGE);
					}
				}	
				
			}
		});
		btnCheckOut.setBounds(420, 23, 108, 23);
		add(btnCheckOut);
		
		JButton btnReject = new JButton("Reject");
		btnReject.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String message = "Comfirm to reject?";
				int reply = JOptionPane.showConfirmDialog(null, message, Config.TITLE_WARNING, JOptionPane.YES_NO_OPTION);
				if (reply == JOptionPane.YES_OPTION) {
					try {
						reject();
					} catch (Exception exception) {
						exception.printStackTrace();
						JOptionPane.showMessageDialog(null, exception.getMessage(), Config.TITLE_ERROR, JOptionPane.ERROR_MESSAGE);
					}
				}	
			}
		});
		btnReject.setBounds(420, 52, 108, 23);
		add(btnReject);
		
		separator = new JSeparator();
		separator.setBounds(10, 87, 485, 5);
		add(separator);
		
		
		lblT = new JLabel("Title:");
		lblT.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		lblT.setBounds(127, 5, 74, 22);
		add(lblT);
		
		lblA = new JLabel("Author:");
		lblA.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		lblA.setBounds(127, 30, 74, 22);
		add(lblA);
		
		label = new JLabel("ISBN:");
		label.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		label.setBounds(127, 55, 74, 22);
		add(label);
		
		label_1 = new JLabel("Borrower:");
		label_1.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		label_1.setBounds(127, 80, 74, 22);
		add(label_1);
	}

	@Override
	public void setEntity(Entity entity) throws Exception {
		loan = (Loan) entity;
		loan.getInfo();
		Item item = loan.getItem();
		item.getInfo();
		lblImage.setIcon(item.getThumbnailBookImage());
		lblTitle.setText   (item.getTitle());
		lblAuthor.setText  (item.getAuthor());
		lblIsbn.setText    (item.getIsbn());
		lblBorrower.setText(loan.getBorrower().getId());
	}

	@Override
	public void accept() throws Exception {
		frame.getCheckOutMgr().checkOut(loan);
		setVisible(false);
		EntityPanelContainer.getContainer(this).reducePanelSize();
		getParent().remove(this);
	}
	
	public void reject() throws Exception {
		frame.getCheckOutMgr().Reject(loan);
		setVisible(false);
		EntityPanelContainer.getContainer(this).reducePanelSize();
		getParent().remove(this);
	}
}
